(function(){
	//获取下拉框类型
	function getlinklist(selectlist,selectid,type){
	   $.ajax({
			url:'http://web.icecn.net/match/getEnumInfo.json',
			type:'POST',
			async:true,
			data:{
			},
			dataType: "json",
			xhrFields:{withCredentials:true},
			success: function(data){
				dataset=data.t;
				var d=new Array();
				if(dataset && dataset.length > 0){
				   for(var i=0;i<dataset.length;i++){
					   if(dataset[i].enumType==type){
						   d.push(dataset[i])
					   }  
				   }
				   dataset=d;
				   var html = template(selectlist,{dataset:dataset});
				   $("#"+selectid).html(html);
				};
			}
	   });	
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			$('.submit').click(function(){
				var matchSystem = $('.matchSystem option:selected').attr("data-id");
				var gameType = $('.gameType option:selected').attr("data-id");
				var serverType = $('.serverType option:selected').attr("data-id");
				var terraceType = $('.terraceType option:selected').attr("data-id");
				var entryFeeType = $('.entryFeeType option:selected').attr("data-id");
				var matchName = $('.matchName').val();
				var minLimit = $('.minLimit').val();
				var minSize = $('.minSize').val();
				var entryFee = $('.entryFee').val();
				var entryTime = $('.entryTime').val() != ''?$('.entryTime').val().substring(0,10)+' '+ $('.entryTime').val().substring(11)+':00':'';
				var endEntryTime = $('.endEntryTime').val() != ''?$('.endEntryTime').val().substring(0,10)+' '+ $('.endEntryTime').val().substring(11)+':00':'';
				var startTime = $('.startTime').val() != ''?$('.startTime').val().substring(0,10)+' '+ $('.startTime').val().substring(11)+':00':'';
				var matchPic = $('.matchPic').val();
				var matchPoster = $('.matchPoster').val();
				var matchIntroduce = CKEDITOR.instances.editor1.getData();
				var gameDay = [];
				for(i = 0; i < $('.gameDay>input:checkbox:checked').length; i++){
					var val = $('.gameDay>input:checkbox:checked').eq(i).val();
					gameDay.push(val);
				};
				var matchPrize = "";
				for(i = 0; i < $('.matchPrize>div').length; i++){
					var label1 = $('.matchPrize>div').eq(i).find('label').html();
					var select1 = $('.matchPrize>div').eq(i).find('select').val();
					var input1 = $('.matchPrize>div').eq(i).find('input').val();
					if(input1!=""){
					matchPrize+="{'ranking':'"+label1+"','awardType':'"+select1+"','awardContent':'"+input1+"'},"}
				};
				matchPrize="["+matchPrize+"]";
				if(matchName==""){alert("赛事名称不能为空");return}
				var data = {
					matchName:matchName,
					matchSystem:matchSystem,
					minLimit:minLimit,
					gameType:gameType,
					minSize:minSize,
					serverType:serverType,
					terraceType:terraceType,
					entryFee:entryFee,
					entryTime:entryTime,
					startTime:startTime,
					endEntryTime:endEntryTime,
					gameDay:gameDay.join(','),
					matchPic:matchPic,
					matchPoster:matchPoster,
					matchIntroduce:matchIntroduce,
					matchPrize:matchPrize,
					entryFeeType:entryFeeType,
					isStick:0
				};
				$.ajax({
					url:'http://web.icecn.net/match/updateMatch.json',
					type:'POST',
					async:true,
					data:data,
					dataType:'json',
					xhrFields:{withCredentials:true},
					success:function(data){
					  if(data.code==200){
						  alert("创建成功")
						  window.location.reload()
					  }else{
						alert(data.msg);  }
					}
				});
			});
			//增加比赛奖励
			$('.plus').click(function(){
				var num = $('.matchPrize>div').length;
				var arr1 = ['一','二','三','四','五','六','七','八','九','十'];
				if(num<=9){
					num1 = arr1[num];
				};
				$('.reward>div:last-of-type').after('<div>' + 
					  '<label data-id="'+ (num+1) +'">第' + num1 + '名</label>'+
					  '<select class="form-control w180">'+
						'<option>钻石</option><option>金币</option>'+
					  '</select>'+
					  '<input type="text" class="form-control w180" placeholder="数量"></div>'
				);
			});
		},
		//渲染页面
		render:function(){
			getlinklist("selectlist","matchSystem",1);
			getlinklist("selectlist","gameType",2);
			getlinklist("selectlist","serverType",5);
			getlinklist("selectlist","terraceType",6);
			getlinklist("selectlist","entryFeeType",3);
		}
	};
	Page.init();
})();